Plant Seedlings Classification

Plant Seedlings – Image classification using CNN & Tensorflow

Data Description:

You are provided with a dataset of images of plant seedlings at various stages of grown. Each image has a filename that is its unique id. The dataset comprises 12 plant species. The goal of the project is to create a classifier capable of determining a plant's species from a photo.

Dataset

The data file names are:

Objective

To build a classification model that capable of determining a plant's species from a photo.

Solution Approach - CNN Image classification

Plant Seedlings Classification

Understand Given Data

Load given data images.npy & Labels.csv to numpy array & data frame and understand data, data type, data nature, features incuded, total records,data has any missing values or duplicate data.

Visualize data and and understand data range

Loading necessary libraries for EDA

Load all standard python library packages.

Data Manipulation

Data Visualization

Time everything in IPython

Load data

Read given images.npy & Labels.csv and load to numpy array & data frame

Load data based on environment, if Colab then load data from google drive else from local system

View the first and last 5 rows of the dataset.

Understand the shape of the dataset.

Observations on data

Exploratory Data Analysis And Data processing

Checking Min & Max Values

Image should range values from 0 to 255 only

All values in images data range from 0 to 255. No invalid data.

Given data has no missing values or duplicates. We dont need to check for outliers in this data.

Labeled barplots

Analysis on Seed Label

Data Distribution

Observations on Seed Label

Collect Index values for Seed Types

Plot the images from each seed class and with their corresponding labels

Let's visualize SEED images randomly from each of the 12 classes. The Image matrix is plotted and each row represents four single channel images corresponding to one class. We have read single channel images in order to reduce complexity.

Importing Tensorflow libraries

Before Data Preprocessing - Different Seed Class Images

Let's visualize SEED images randomly from each of the 12 classes.

Observation

Smoothing techniques on Images

Denoising techniques - Opencv library

  1. Fast Nl Means Denoising Colored
  2. Gaussian Blur
  3. Median Blur
  4. Bilateral Filter

Crop Image with OpenCV

lets crop edges so we can remove some of the tape/barcode present in the image

Crop Observations

lets do 5% edge crop to lose 5% edges so that image has maximum data and no data loss

Image Denoising & Convert to grayscale - Data Pre - Processing

Converting image to grayscale to reduce the complexity and computation

Post Data Preprocessing & Cleaning - Different Seed Class Images

Let's visualize SEED images in RBG randomly from each of the 12 classes. After Denoising

We see that RGB dimensions are reduced to 1 dimension gray scale images

Let's visualize SEED images in gray scale randomly from each of the 12 classes.

Observation

Finding the mean image for each seed class:

Finding the mean images - RGB

Finding the mean image - Gray Scale

We cannot see any seed in Mean images because of different image types and stones, bar code tapes

Data Preprocessing

Data Preparation for Modeling

Normalize data - Make data compatible

Splitting data into training and test

One-Hot Encoding

Insights based on EDA

Model Building

Common methods used as callback to

Mode1 1- ANN (Artificial Neural Network - fully connected)

Build Model

Model Summary

Train Model

Model Performance Evaluation

Model Loss, Accuracy, Recall & Other Scores

Plotting the confusion matrix for the model

Model Observation

As we see here, the ANN does not show a good test accuracy, since ANNs are unable to capture spatial correlation characteristics of the image.

Let's try Convolutional Neural Networks, which take in the whole image as a 2D matrix instead. CNNs tend to behave well on image data but the important point to consider is, it may not be true until an optimized CNN is built depending on the data.

Model 2 - Convolutional Neural Network (CNN)

Lets try a CNN model with Dropout

Build Model

Model Summary

Train Model

Model Performance Evaluation

Model Loss, Accuracy, Recall & Other Scores

Plotting the confusion matrix for the model

Model Observation

Here we can observe that this is performing better than ANN but we will try to optimize this model and improve its performance.

This model unfortunately does not have a good test accuracy

Model 3 - Convolutional Neural Network (CNN)

Since CNN Model 1 does not appear to have good test accuracy and appears to be overfitting on the training dataset, let's build new CNN Model 2, which has a different architecture that should generalize well and not overfit.

Model Performance Improvement

Lets try a CNN model with multiple Convolutional layers, Dropout and multiple Dense Layers

Build Model

Model Summary

Train Model

Model Performance Evaluation

Model Loss, Accuracy, Recall & Other Scores

Plotting the confusion matrix for the model

Model Observation

There is still scope for improvement in the test accuracy and F1 score of the CNN model

Model Performance Evaluation - Comparing all Models

Conclusion and key takeaways

As we have seen, ANNs do not work well with image data, because ANNs do not take 2-D images as input. They flatten the image and make it lose its spatial struture, whereas CNNs take the full 2D-image as input in order to perform feature extraction. So CNNs do not lose the image's spatial structure, which makes them more suitable for working with image datasets.